package com.lw.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.lw.common.aop.LogOperation;
import com.lw.common.enums.LogModuleEnum;
import com.lw.common.utils.ResultVo;
import com.lw.common.utils.StrUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.lw.entity.TbDictItem;
import com.lw.service.TbDictItemService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static com.lw.entity.table.TbDictItemTableDef.TB_DICT_ITEM;

/**
 * 字典项表 控制层。
 *
 * @author lw
 * @since 2025年05月17日 07:58:04
 */
@RestController
@Tag(name = "字典项表接口")
@RequestMapping("/tbDictItem")
public class TbDictItemController {

    @Resource
    private TbDictItemService tbDictItemService;

    /**
     * 添加字典项表。
     *
     * @param tbDictItem 字典项表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("saveItem")
    @Operation(description="保存字典项表")
    @SaCheckPermission("sys:dict-item:add")
    @LogOperation(module = LogModuleEnum.LOG_DICT, value = "添加字典项表", params = false,result = false)
    public ResultVo<Boolean> saveItem(@RequestBody @Parameter(description="字典项表")TbDictItem tbDictItem) {
        tbDictItem.setCreateBy(StpUtil.getLoginIdAsLong());
        // 检查是否已经存在相同的字典项
        if(StrUtils.isEmpty(tbDictItem.getDictCode())){
            return ResultVo.error("字典码不能为空");
        }
        return ResultVo.success(tbDictItemService.saveItem(tbDictItem));
    }

    /**
     * 根据主键删除字典项表。
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("deleteDictItem/{ids}")
    @Operation(description="根据主键字典项表")
    @SaCheckPermission("sys:dict-item:delete")
    @LogOperation(module = LogModuleEnum.LOG_DICT, value = "根据主键删除字典项表", params = false,result = false)
    public ResultVo<Boolean> deleteDictItem(@PathVariable("ids") @Parameter(description="字典项表主键")String ids) {
        if (ids == null || ids.isEmpty()) {
            return ResultVo.error("主键不能为空");
        }
        List<Long> idsList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        return ResultVo.success(tbDictItemService.removeByIds(idsList));
    }

    /**
     * 根据主键更新字典项表。
     *
     * @param tbDictItem 字典项表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("updateItem")
    @Operation(description="根据主键更新字典项表")
    @SaCheckPermission("sys:dict-item:edit")
    @LogOperation(module = LogModuleEnum.LOG_DICT, value = "根据主键更新字典项表", params = false,result = false)
    public ResultVo<Boolean> updateItem(@RequestBody @Parameter(description="字典项表主键")TbDictItem tbDictItem) {
        tbDictItem.setUpdateBy(StpUtil.getLoginIdAsLong());
        return ResultVo.success(tbDictItemService.updateItem(tbDictItem));
    }

    /**
     * 查询所有字典项表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @Operation(description="查询所有字典项表")
    public ResultVo<List<TbDictItem>> list() {
        return ResultVo.success(tbDictItemService.list());
    }

    /**
     *  根据字典编码查询字典项表。
     * @param dictCode 字典码
     * @return 所有数据
     */
    @GetMapping("getDictItemList/{dictCode}")
    @Operation(description="根据字典编码查询字典项表")
    public ResultVo<List<TbDictItem>> list(@PathVariable("dictCode") String dictCode) {
        if (dictCode == null || dictCode.isEmpty()) {
            return ResultVo.error("字典编码不能为空");
        }
        return ResultVo.success(tbDictItemService.listByDictCode(dictCode));
    }

    /**
     * 根据字典项表主键获取详细信息。
     *
     * @param id 字典项表主键
     * @return 字典项表详情
     */
    @GetMapping("getInfo/{id}")
    @Operation(description="根据主键获取字典项表")
    public ResultVo<TbDictItem> getInfo(@PathVariable("id") Long id) {
        return ResultVo.success(tbDictItemService.getById(id));
    }

    /**
     * 分页查询字典项表。
     *
     * @return 分页对象
     */
    @GetMapping("/{dictCode}/getDictItemPage")
    @Operation(description="分页查询字典项表")
    public ResultVo<Page<TbDictItem>> getDictItemPage(@Parameter(description="分页信息")TbDictItem dictItem,@PathVariable("dictCode") String dictCode) {
        dictItem.setDictCode(dictCode);
        return ResultVo.success(tbDictItemService.getDictItemPage(dictItem));
    }

}
